export function debounce(fn,wait){
    let timer = null;
    // 返回函数对比debounce作用域形成闭包
    return function(){
        let context = this;
        let args = arguments;
        if(timer){
            // 事件被触发，清楚timer并重新开始计时
            clearTimeout(timer);
            timer = null;
        }
        timer = setTimeout(function(){
            fn.apply(context,args)
        },wait)
    }
}
export function trottle (func, wait) {
    let context, args, timeout
    let old = 0 // 时间戳
    let later = function () {
      old = new Date().valueOf()
      timeout = null
      func.apply(context, args)
    }
    return function () {
      context = this
      args = arguments
      let now = new Date().valueOf()
      if (now - old > wait) {
        if (timeout) {
          clearTimeout(timeout)
          timeout = null 
        }
        func.apply(context, args)
        old = now
      }
  
      if (!timeout) {
        timeout = setTimeout(later, wait)
      }
    }
}